import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_todo_2019/common/application.dart';
import 'package:flutter_todo_2019/common/common.dart';
import 'package:flutter_todo_2019/common/router_config.dart';
import 'package:flutter_todo_2019/common/user.dart';
import 'package:flutter_todo_2019/event/theme_change_event.dart';
import 'package:flutter_todo_2019/page/api/apis_service.dart';
import 'package:flutter_todo_2019/page/callback/MyCallback.dart';
import 'package:flutter_todo_2019/page/color/circle_page.dart';
import 'package:flutter_todo_2019/page/color/ring_color_pick.dart';
import 'package:flutter_todo_2019/page/color/ring_page.dart';
import 'package:flutter_todo_2019/page/login/login_screen.dart';
import 'package:flutter_todo_2019/page/mode/base_model.dart';
import 'package:flutter_todo_2019/page/utils/item_utils.dart';
import 'package:flutter_todo_2019/page/utils/route_util.dart';
import 'package:flutter_todo_2019/page/utils/sp_util.dart';
import 'package:flutter_todo_2019/page/utils/theme_util.dart';
import 'package:flutter_todo_2019/page/utils/toast_util.dart';
import 'package:flutter_todo_2019/res/colors.dart';
import 'package:flutter_todo_2019/test/imooc/RouteNavigator.dart';

/// 设置页面   有状态的Widget
class SettingScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new SettingScreenState();
  }
}
///有状态的Page
class SettingScreenState extends State<SettingScreen> implements MyCallback {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          elevation: 0.4,
          title:
              Text('设置', style: TextStyle(color: Colors.white, fontSize: 18)),
          centerTitle: true,
          leading: GestureDetector(
            onTap: () {
              Navigator.pop(context);
            },
            child: Icon(Icons.arrow_back,color: Colors.white),
          )),
      body: ListView(
        children: <Widget>[
          new ExpansionTile(
            title: new Row(
              children: <Widget>[
                Icon(Icons.color_lens, color: Theme.of(context).primaryColor),
                Padding(
                  padding: EdgeInsets.only(left: 10.0),
                  child: Text('主题一'),
                )
              ],
            ),
            children: <Widget>[
              new Wrap(
                children: themeColorMap.keys.map((String key) {
                  Color value = themeColorMap[key];
                  return new InkWell(
                    onTap: () {
                      SPUtil.putString(Constants.THEME_COLOR_KEY, key);
                      ThemeUtils.currentThemeColor = value;
                      Application.eventBus.fire(ThemeChangeEvent());
                    },
                    child: new Container(
                      margin: EdgeInsets.all(5.0),
                      width: 36.0,
                      height: 36.0,
                      color: value,
                    ),
                  );
                }).toList(),
              )
            ],
          ),
          Divider(),
          ItemUtils.getItemView(
              context, "主题二", SettingPage.THEME_SELECTED_ONE, this),
          Divider(),
          ItemUtils.getItemView(
              context, "主题三", SettingPage.THEME_SELECTED_TWO, this),
          Divider(),
          ItemUtils.getItemView(context, "关于Flags", SettingPage.ABOUT, this),
          Divider(),
          ItemUtils.getItemView(context, "仿微信读书", SettingPage.DEMOS, this),
          Divider(),
          ItemUtils.getItemView(context, "一起来学习", SettingPage.LEARN, this),
          Divider(),
          ItemUtils.getItemView(context, "退出登录", SettingPage.EXITOUT, this),
        ],
      ),
    );
  }

  @override
  void onCallback(String result) {
    switch (result) {
      case SettingPage.ABOUT:
        RouteUtil.toWebView(context, "关于Flags功能", Constants.About_Str);
        break;
      case SettingPage.DEMOS:
        Router.navigatorKey.currentState.pushNamed(RouterName.wx_read);
        break;
      case SettingPage.LEARN:
        jumpDemoPage(context);

        break;
      case SettingPage.THEME_SELECTED_ONE:
        RouteUtil.jumpPage(context, new RingPage());
        break;
      case SettingPage.THEME_SELECTED_TWO:
        RouteUtil.jumpPage(context, new CirclePage());
        break;
      case SettingPage.EXITOUT:
        _logout(context);
        break;
    }
  }
}

/// 退出登录
void _logout(BuildContext context) {
  showDialog(
    context: context,
    builder: (context) => new AlertDialog(
      // title: new Text(''),
      content: new Text('确定退出登录吗？'),
      actions: <Widget>[
        new FlatButton(
          onPressed: () => Navigator.of(context).pop(false),
          child: new Text('取消', style: TextStyle(color: Colors.cyan)),
        ),
        new FlatButton(
          onPressed: () {
            requestLogOut(context);
          },
          child: new Text('确定', style: TextStyle(color: Colors.cyan)),
        ),
      ],
    ),
  );
}

requestLogOut(BuildContext context) {
  apiService.logout((BaseModel model) {
    Navigator.of(context).pop(true);
    if (model.errorCode == Constants.STATUS_SUCCESS) {
      User.singleton.clearUserInfo();
      T.show(msg: '已退出登录');
      //            TODO 跳转
      Navigator.of(context).pushAndRemoveUntil(
          new MaterialPageRoute(builder: (context) => LoginScreen()),
          (route) => route == null);
    } else {
      T.show(msg: model.errorMsg);
    }
  }, (DioError error) {
    print(error.response);
  });
}

jumpDemoPage(BuildContext context) {
  Navigator.of(context)
      .push(new MaterialPageRoute(builder: (BuildContext context) {
    return new RouteNavigator();
  }));
}
